import {editArticle, infoArticle} from '@/api/article'
import {Tag, tagsInfo} from '@/api/tag'
import {env} from '@/utils/env'
import {reactive} from 'vue'
import {Article} from '@/api/article/type'
import {commentCreate, commentInfo} from '@/api/comment'
import {Comment} from '@/api/comment/type'
import {follow} from '@/api/follow'
import {changeStar} from '@/api/like'
import {useUserStore} from "@/store/modules/user";
const userStore = useUserStore();
const useArticleDetail = () => {
    const detailData = reactive({
        detail: {
            user_info: {},
        } as Article.ArticleInfo,

        detailEdit: {
            user_info: {},
        } as Article.ArticleInfo,

        tagInfo: [] as Tag.TagInfo[],
        cardInfo: {} as Article.CardInfo,
        isFollow: false,
        isStar: false,
        isShowEdit: false,
    })
    const commentData = reactive({
        list: [] as Comment.CommentInfo[],
        content: '',
    })
    const getArticleDetail = (uid: number, userId: number, category: number,queryType: string) => {
        let params = {
            uid: uid,
            user_id: userId,
            category: category,
            queryType: queryType
        }
        infoArticle(params).then((res) => {
            detailData.detail = res.data.article_info.map((a) => {
                return {
                    ...a,
                    cover: `${env.VITE_APP_IMG_URL}/${a.cover}`,
                    user_info: {
                        ...a.user_info,
                        avatar_url: `${env.VITE_APP_IMG_URL}/${a.user_info.avatar_url}`,
                    },
                }
            })[0]

            detailData.detailEdit = {...detailData.detail}
            detailData.cardInfo = res.data.card_info
            detailData.isFollow = res.data.card_info.follow_ids.includes(userStore.info.id)
            detailData.isStar = res.data.card_info.star_ids.includes(userStore.info.id)
            getTagInfo(res.data.article_info[0].tags)
        })
    }

    const getComments = (uid: number) => {
        let params = {
            page: 1,
            limit: 10,
            type: 'article',
            content_id: uid,
        }
        commentInfo(params).then((res) => {
            commentData.list = res.data.infos.map(info => {
                return {
                    ...info,
                    user_info: {
                        ...info.user_info,
                        avatar_url: `${env.VITE_APP_IMG_URL}/${info.user_info.avatar_url}`,
                    },
                }
            })
        })
    }

    const getTagInfo = (uids: string) => {
        tagsInfo({type: 'article', uids: uids}).then((res) => {
            detailData.tagInfo = res.data.tags_info
        })
    }

    const createComment = async (uid: number, userId: number) => {
        if (commentData.content.trim() == '') {
            window.$message.error('评论内容不能为空')
            return
        }
        let data = {
            content_id: uid,
            createdUserId: userId,
            content: commentData.content,
            type: 'article',
        }
        await commentCreate(data)
        commentData.content = ''
        getComments(uid)
        getArticleDetail(uid, userId,1,'detail')
    }

    const addFollow = async () => {
        const params = {
            follow_id: detailData.detail.user_info.id,
            follow_type: detailData.isFollow ? 0 : 1,
        } as const
        await follow(params)
        detailData.isFollow = !detailData.isFollow
    }

    const imageStar = async (uid: number) => {
        const data = {
            uid: uid,
            createdUserId: detailData.detail.user_info.id,
            star_type: detailData.isStar ? 0 : 1,
            type: 3,
        }
        await changeStar(data)
        // detailData.isStar = !detailData.isStar
        getArticleDetail(detailData.detail.uid, detailData.detail.user_info.id,1,'detail')
    }

    const editSubmit = async () => {
        let data = {
            uid: detailData.detail.uid,
            title: detailData.detail.title,
            sub_title: detailData.detail.sub_title,
            content: detailData.detailEdit.content,
        }

        await editArticle(data)
        detailData.detail = {...detailData.detailEdit}
        return true
    }

    return {
        detailData,
        commentData,
        getArticleDetail,
        getComments,
        createComment,
        addFollow,
        imageStar,
        editSubmit,
    }
}

export {
    useArticleDetail,
}